using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace LibGreen.Jabber
{
    /// <summary>
    /// Represents a group from the client's roster
    /// </summary>
    public class RosterGroup : IComparable
    {
        private readonly string name;
        private readonly List<RosterItem> items = new List<RosterItem>();

        /// <summary>
        /// Initializes a new <see cref="RosterGroup"/>
        /// </summary>
        /// <param name="name">The name of the group</param>
        public RosterGroup(string name)
        {
            this.name = name;
        }

        /// <summary>
        /// Gets the name of the group
        /// </summary>
        public string Name
        {
            get { return name; }
        }

        /// <summary>
        /// Gets a list of items which belong to this group
        /// </summary>
        public ReadOnlyCollection<RosterItem> Items
        {
            get { return new ReadOnlyCollection<RosterItem>(items); }
        }

        /// <summary>
        /// Adds a new item to the list of items which belong to this group
        /// </summary>
        /// <param name="item">The item to add</param>
        internal void AddItem(RosterItem item)
        {
            if (!items.Contains(item))
            {
                items.Add(item);
            }
        }

        #region IComparable Members
        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="obj">An object to compare with this instance.</param>
        /// <returns>A 32-bit signed integer that indicates the relative order of the objects being compared.</returns>
        public int CompareTo(object obj)
        {
            RosterGroup other = obj as RosterGroup;
            if (other != null)
            {
                return Name.CompareTo(other.Name);
            }
            return -1;
        }

        /// <summary>
        /// Determines whether the specified Object is equal to the current Object.
        /// </summary>
        /// <param name="obj">The Object to compare with the current Object.</param>
        /// <returns>true if the specified Object is equal to the current Object; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            return CompareTo(obj) == 0;
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>A hash code for the current object</returns>
        public override int GetHashCode()
        {
            return name.GetHashCode();
        }
        #endregion
    }
}
